<?php
include_once 'root.php';
Class Promotion extends Root {
	public function __construct() {
		parent::__construct();
		$this -> load -> helper('util');
	}

	public function index() {
		$this -> view();
	}

	public function view() {

		//$this->checkAuthen("promotion","view");

		$this -> data['header'] = "รายชื่อสินค้า";
		$this -> addJS(array("jquery.ui", "datethai"));

		$this -> pagination -> create_links();
		$this -> data['canSearch'] = true;
		$q = $this -> getCoreQuery($this -> data['canSearch']);
		
		$this -> load -> model("promotionmodel");
		$allRow = $this -> promotionmodel -> getCountPromotion($q);
		$pageConfig = $this -> initPagination($allRow);
		;
		$this -> data['pagination'] = $this -> pagination;
		$this -> data['dataList'] = $this -> promotionmodel -> getPromotion($pageConfig['start'], $pageConfig['limit'], $q);
		$this -> includeView("viewpromotion");
	}

	public function viewsmat($PromoID) {

		$this -> data['header'] = "รายการสินค้าขาย";
		$ordermodel = $this -> load -> model("promotionmodel");
		$this -> pagination -> create_links();
		$this -> load -> model("promotionmodel");
		$allRow = $this -> promotionmodel -> getPromotionSmatCount($PromoID);
		$pageConfig = $this -> initPagination($allRow);
		;
		$this -> data['pagination'] = $this -> pagination;

		$this -> data['dataList'] = $this -> promotionmodel -> getPromotionSmat($PromoID, $pageConfig['start'], $pageConfig['limit']);
		$this -> data['PromoID'] = $PromoID;
		$this -> includeView("viewsmat");

	}

	public function viewfmat($PromoID) {

		$this -> data['header'] = "รายการสินค้าแถม";
		$ordermodel = $this -> load -> model("promotionmodel");
		$this -> pagination -> create_links();
		$this -> load -> model("promotionmodel");
		$allRow = $this -> promotionmodel -> getPromotionFmatCount($PromoID);
		$pageConfig = $this -> initPagination($allRow);
		;
		$this -> data['pagination'] = $this -> pagination;

		$this -> data['dataList'] = $this -> promotionmodel -> getPromotionFmat($PromoID, $pageConfig['start'], $pageConfig['limit']);
		$this -> data['PromoID'] = $PromoID;
		$this -> includeView("viewfmat");

	}

	public function viewpromolevel($PromoID) {

		$this -> data['header'] = "จำนวนซื้อ/ของแถม/ส่วนลด";
		$ordermodel = $this -> load -> model("promotionmodel");
		$this -> pagination -> create_links();
		$this -> load -> model("promotionmodel");
		$allRow = $this -> promotionmodel -> getPromoLevelCount($PromoID);
		$pageConfig = $this -> initPagination($allRow);
		;
		$this -> data['pagination'] = $this -> pagination;

		$this -> data['dataList'] = $this -> promotionmodel -> getPromoLevel($PromoID, $pageConfig['start'], $pageConfig['limit']);
		$this -> data['PromoID'] = $PromoID;
		$this -> includeView("viewpromolevel");

	}

	public function printSmat($PromoID) {

		$this -> load -> model("promotionmodel");
		$smat = $this -> promotionmodel -> getSmat($PromoID);

		$detail = $smat -> smatDetail;

		$this -> load -> helper('pdf_helper');
		tcpdf();

		$x = 5;
		$y = 10;
		$dimen = 5;

		$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

		// set default header data
		
		$pdf -> setFooterDataExt(array(0, 64, 0), array(0, 64, 128),"Promotion ID: " . $PromoID. ". Print date ".date("Y-m-d H:i:s")." ");

		// set header and footer fonts
		$pdf -> setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
		$pdf -> setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

		// set default monospaced font
		$pdf -> SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

		// set margins
		$pdf -> SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP-20, PDF_MARGIN_RIGHT);
		$pdf -> SetHeaderMargin(PDF_MARGIN_HEADER);
		$pdf -> SetFooterMargin(PDF_MARGIN_FOOTER);
		// set margins

		// disable header and footer
		$pdf -> setPrintHeader(true);
		$pdf -> setPrintFooter(true);
		$pdf -> AddPage();

		//Header
		$pdf -> SetFillColor(255);
		$pdf -> SetTextColor(0);
		$pdf -> SetFont('angsanaupc', '', 24, '', true);
		$pdf -> Cell(180, 7, "รายการสินค้าขาย Promotion ID: " . $PromoID, 0, 0, 'C', 1);
		$pdf -> SetFont('angsanaupc', '', 16, '', true);
		$header = array('รหัสสินค้า', 'รายละเอียดสินค้า');
		$w = array(90, 90);
		$num_headers = count($header);
		$pdf -> Ln();
		$pdf -> Ln();
		// Color and font restoration
		$pdf -> SetFillColor(204, 51, 0);
		$pdf -> SetTextColor(255, 255, 255);
		$pdf -> SetFont('');
		for ($i = 0; $i < $num_headers; ++$i) {
			//echo $header[$i];exit();
			$pdf -> Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
		}
		$data = $smat -> smatDetail;

		if (!empty($data)) {
			foreach ($data as $in => $val) {
				$pdf -> Ln();
				$pdf -> SetFillColor(255);
				$pdf -> SetTextColor(0);
				$pdf -> Cell($w[0], 7, $val -> MATNR, 1, 0, 'L', 1);
				$pdf -> Cell($w[1], 7, $val -> MAKTX, 1, 0, 'L', 1);
			}
		}

		$pdf -> Output('printsmat' . $PromoID . '.pdf', 'I');
		$this -> load -> view('printsmat');

	}
	
	public function printFmat($PromoID) {

		$this -> load -> model("promotionmodel");
		$fmat = $this -> promotionmodel -> getFmat($PromoID);

		$detail = $fmat -> fmatDetail;

		$this -> load -> helper('pdf_helper');
		tcpdf();

		$x = 5;
		$y = 10;
		$dimen = 5;

		$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

		// set default header data
		
		$pdf -> setFooterDataExt(array(0, 64, 0), array(0, 64, 128),"Promotion ID: " . $PromoID. ". Print date ".date("Y-m-d H:i:s")." ");

		// set header and footer fonts
		$pdf -> setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
		$pdf -> setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

		// set default monospaced font
		$pdf -> SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

		// set margins
		$pdf -> SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP-20, PDF_MARGIN_RIGHT);
		$pdf -> SetHeaderMargin(PDF_MARGIN_HEADER);
		$pdf -> SetFooterMargin(PDF_MARGIN_FOOTER);
		// set margins

		// disable header and footer
		$pdf -> setPrintHeader(true);
		$pdf -> setPrintFooter(true);
		$pdf -> AddPage();

		//Header
		$pdf -> SetFillColor(255);
		$pdf -> SetTextColor(0);
		$pdf -> SetFont('angsanaupc', '', 24, '', true);
		$pdf -> Cell(180, 7, "รายการสินค้าแถม Promotion ID: " . $PromoID, 0, 0, 'C', 1);
		$pdf -> SetFont('angsanaupc', '', 16, '', true);
		$header = array('รหัสสินค้า', 'รายละเอียดสินค้า');
		$w = array(90, 90);
		$num_headers = count($header);
		$pdf -> Ln();
		$pdf -> Ln();
		// Color and font restoration
		$pdf -> SetFillColor(204, 51, 0);
		$pdf -> SetTextColor(255, 255, 255);
		$pdf -> SetFont('');
		for ($i = 0; $i < $num_headers; ++$i) {
			//echo $header[$i];exit();
			$pdf -> Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
		}
		$data = $fmat -> fmatDetail;

		if (!empty($data)) {
			foreach ($data as $in => $val) {
				$pdf -> Ln();
				$pdf -> SetFillColor(255);
				$pdf -> SetTextColor(0);
				$pdf -> Cell($w[0], 7, $val -> MATNR, 1, 0, 'L', 1);
				$pdf -> Cell($w[1], 7, $val -> MAKTX, 1, 0, 'L', 1);
			}
		}

		$pdf -> Output('printfmat' . $PromoID . '.pdf', 'I');
		$this -> load -> view('printfmat');

	}
	
	public function printPromoLevel($PromoID) {

		$this -> load -> model("promotionmodel");
		$promolevel = $this -> promotionmodel -> getPLevel($PromoID);

		$detail = $promolevel -> promolevelDetail;

		$this -> load -> helper('pdf_helper');
		tcpdf();

		$x = 5;
		$y = 10;
		$dimen = 5;

		$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

		// set default header data
		
		$pdf -> setFooterDataExt(array(0, 64, 0), array(0, 64, 128),"Promotion ID: " . $PromoID. ". Print date ".date("Y-m-d H:i:s")." ");

		// set header and footer fonts
		$pdf -> setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
		$pdf -> setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

		// set default monospaced font
		$pdf -> SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

		// set margins
		$pdf -> SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP-20, PDF_MARGIN_RIGHT);
		$pdf -> SetHeaderMargin(PDF_MARGIN_HEADER);
		$pdf -> SetFooterMargin(PDF_MARGIN_FOOTER);
		// set margins

		// disable header and footer
		$pdf -> setPrintHeader(true);
		$pdf -> setPrintFooter(true);
		$pdf -> AddPage();

		//Header
		$pdf -> SetFillColor(255);
		$pdf -> SetTextColor(0);
		$pdf -> SetFont('angsanaupc', '', 24, '', true);
		$pdf -> Cell(180, 7, "จำนวนซื้อ/ของแถม/ส่วนลด Promotion ID: " . $PromoID, 0, 0, 'C', 1);
		$pdf -> SetFont('angsanaupc', '', 16, '', true);
		$header = array('ลำดับ', 'จำนวนที่ต้องสั่ง','หน่วย','จำนวนของแถม','หน่วย','จำนวนส่วนลดต่อลัง','ส่วนลด %');
		$w = array(15, 35, 15, 35, 15, 40, 20);
		$num_headers = count($header);
		$pdf -> Ln();
		$pdf -> Ln();
		// Color and font restoration
		$pdf -> SetFillColor(204, 51, 0);
		$pdf -> SetTextColor(255, 255, 255);
		$pdf -> SetFont('');
		for ($i = 0; $i < $num_headers; ++$i) {
			//echo $header[$i];exit();
			$pdf -> Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
		}
		$data = $promolevel -> promolevelDetail;

		if (!empty($data)) {
			foreach ($data as $in => $val) {
				$pdf -> Ln();
				$pdf -> SetFillColor(255);
				$pdf -> SetTextColor(0);
				$pdf -> Cell($w[0], 7, $val -> PROMO_LEVEL, 1, 0, 'C', 1);
				$pdf -> Cell($w[1], 7, $val -> ORDER_QTY, 1, 0, 'C', 1);
				$pdf -> Cell($w[2], 7, getUnitDesc($val->ORDER_UOM), 1, 0, 'C', 1);
				$pdf -> Cell($w[3], 7, $val -> FREE_QTY, 1, 0, 'C', 1);
				$pdf -> Cell($w[4], 7, getUnitDesc($val->FREE_UOM), 1, 0, 'C', 1);
				$pdf -> Cell($w[5], 7, $val -> DISC_AMT, 1, 0, 'C', 1);
				$pdf -> Cell($w[6], 7, $val -> DISC_PERC, 1, 0, 'C', 1);
			}
		}

		$pdf -> Output('printpromolevel' . $PromoID . '.pdf', 'I');
		$this -> load -> view('printpromolevel');

	}

}
?>